﻿' <summary>
' BeeWit IDE Entorno de desarrollo Visual para php.
' BeeWitPHP IDE es el único entorno de desarrollo para PHP 
' de tipo WYSIWYG (What You See Is What You Get). 
' ¡Ahora si!, aproveche la potencia del lenguaje PHP 
' y desarrolle más rápidamente gracias a la programación 
' intuitiva de BeeWit.
' Copyright © 2010 Dewins Murillo Garcia 
' EMAIL: dewinsmg@gmail.com
' EMAIL: dewins@beewitsoft.com
' Este Programa es Software Libre: usted puede redistribuirlo 
' y/o modificarlo bajo los términos de la Licencia Publica General 
' GNU como es publicada por la Fundacion de Software Libre; 
' en la 3ra versión de la licencia.
' Este programa es distribuido con la esperanza de que sea útil, 
' pero SIN GARANTÍA ALGUNA; sin siquiera la garantía implícita 
' de VALOR COMERCIAL o FORMADO PARA UN PROPÓSITO EN PARTICULAR. 
' Vea la Licencia Publica General GNU para mas detalles.
' Usted debe haber recibido una copia de la Licencia Publica General 
' GNU junto con este programa. Si no, vaya a http://www.beewitsoft.com
' o en http://code.google.com/p/ajax-beewit-php-ide/
' </summary>
' <remarks></remarks>

Public Class ctlMenu

    'Función Api GetAsyncKeyState
    Private Declare Function GetAsyncKeyState _
        Lib "user32" ( _
            ByVal vKey As Long) As Integer

    Private swTimer As Long = 0
    Private Const WM_KEYDOWN = &H100
    Private Const WM_KEYUP = &H101
    Private Const WM_MOUSEMOVE = &H200
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    Private Const WM_LBUTTONDBLCLK = &H203
    Private Const WM_RBUTTONDOWN = &H204
    Private Const WM_RBUTTONUP = &H205
    Private Const WM_RBUTTONDBLCLK = &H206
    Private Const WM_MBUTTONDOWN = &H207
    Private Const WM_MBUTTONUP = &H208
    Private Const WM_MBUTTONDBLCLK = &H209
    Private Const WM_MOUSEWHEEL = &H20A
    Private Const WM_SYSTEMKEYDOWN = &H104
    Private Const WM_SYSTEMKEYUP = &H105
    Private _objDebug As New clsDebug

    Public Sub New()
        ' Llamada necesaria para el Diseñador de Windows Forms.
        InitializeComponent()
        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
    End Sub

    Private Sub ctlMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.menu_file.Text = Object_Language.Lang_Text("File")
        Me.menu_new_project.Text = Object_Language.Lang_Text("New Project")
        Me.menu_open_project.Text = Object_Language.Lang_Text("Open Project")

        Me.TSFiles.ImageList = Object_Icons.bmpImageList
        Me.TSLabelsTag.ImageList = Object_Icons.bmpImageList
        Me.TSCompile.ImageList = Object_Icons.bmpImageList
        Me.MenuStrip1.ImageList = Object_Icons.bmpImageList

        Me.btnSave.ToolTipText = Object_Language.Lang_Text("Save")
        Me.btnSave.ImageKey = "icon_save"

        Me.btnSaveAll.ToolTipText = Object_Language.Lang_Text("Save All")
        Me.btnSaveAll.ImageKey = "icon_save_all"

        Me.btnCopy.ToolTipText = Object_Language.Lang_Text("Copy")
        Me.btnCopy.ImageKey = "icon_copy"

        Me.btnPaste.ToolTipText = Object_Language.Lang_Text("Paste")
        Me.btnPaste.ImageKey = "icon_paste"

        Me.btnVerify.ToolTipText = Object_Language.Lang_Text("Verify")
        Me.btnVerify.ImageKey = "icon_verify"
        Me.btnVerify.Enabled = True

        Me.btnDebug.ToolTipText = Object_Language.Lang_Text("Debug")
        Me.btnDebug.ImageKey = "icon_debug"
        'Me.btnDebug.Enabled = False
        Me.btnStop.ImageKey = "icon_stop"
        Me.btnInspect.ImageKey = "icon_inspect"

        Me.btnUndo.ToolTipText = Object_Language.Lang_Text("Undo")
        Me.btnUndo.ImageKey = "icon_undo"
        Me.btnUndo.Enabled = True

        Me.btnRedo.ToolTipText = Object_Language.Lang_Text("Redo")
        Me.btnRedo.ImageKey = "icon_redo"
        Me.btnRedo.Enabled = True


        Me.menu_about.Text = Object_Language.Lang_Text("About")
        Me.menu_error_notifications.Text = Object_Language.Lang_Text("Error Notifications")
        Me.menu_faqs.Text = Object_Language.Lang_Text("FAQs")
        Me.menu_samples.Text = Object_Language.Lang_Text("Samples")
        Me.menu_technical_manual.Text = Object_Language.Lang_Text("Technical Manual")
        Me.menu_user_manual.Text = Object_Language.Lang_Text("User Manual")
        Me.menu_help.Text = Object_Language.Lang_Text("Help")
        Me.ExitToolStripMenuItem.Text = Object_Language.Lang_Text("Exit")

        Me.StepIntoToolStripMenuItem.Text = Object_Language.Lang_Text("Step Into")
        Me.StepOutToolStripMenuItem.Text = Object_Language.Lang_Text("Step Out")
        Me.StepOverToolStripMenuItem.Text = Object_Language.Lang_Text("Step Over")
        Me.StopDebuggingToolStripMenuItem.Text = Object_Language.Lang_Text("Stop Debug")
        Me.ListenToolStripMenuItem.Text = Object_Language.Lang_Text("Run")
        Me.InspectToolStripMenuItem.Text = Object_Language.Lang_Text("Inspect")

        Me.ListenToolStripMenuItem.Image = Object_Icons.bmpImage("icon_debug")
        Me.StopDebuggingToolStripMenuItem.Image = Object_Icons.bmpImage("icon_stop")

        Me.StepIntoToolStripMenuItem.Image = Object_Icons.bmpImage("icon_step_into")
        Me.StepOverToolStripMenuItem.Image = Object_Icons.bmpImage("icon_step_over")
        Me.StepOutToolStripMenuItem.Image = Object_Icons.bmpImage("icon_step_out")
        Me.InspectToolStripMenuItem.Image = Object_Icons.bmpImage("icon_inspect")

        Me.menu_new_project.Image = Object_Icons.bmpImage("icon_new_project")
        Me.menu_open_project.Image = Object_Icons.bmpImage("icon_open_directory")
        Me.SaveFileToolStripMenuItem.Image = Object_Icons.bmpImage("icon_save")

        Me.viewToolStripMenuItem.Text = Object_Language.Lang_Text("View")
        Me.debugToolStripMenuItem1.Text = Object_Language.Lang_Text("Debug")
        Me.DebugToolStripMenuItem.Text = Object_Language.Lang_Text("Debug")
        Me.projectToolStripMenuItem.Text = Object_Language.Lang_Text("Project")
        Me.controlsToolStripMenuItem.Text = Object_Language.Lang_Text("Controls")
        Me.consoleToolStripMenuItem.Text = Object_Language.Lang_Text("Console")
        Me.eventsToolStripMenuItem.Text = Object_Language.Lang_Text("Events")
        Me.propertiesToolStripMenuItem.Text = Object_Language.Lang_Text("Properties")

        AddLabelsTag()

    End Sub

    Private Sub menu_new_project_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menu_new_project.Click
        Control_NewOpenProjects.CreateNewProject()
    End Sub

    Private Sub menu_open_project_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menu_open_project.Click
        Control_NewOpenProjects.OpenProject()
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Control_Main.Save()
    End Sub

    Private Sub btnSaveAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAll.Click
        Control_Main.SaveAll()
    End Sub

    Private Sub AddLabelsTag()
        Try
            ListFonts()
            ListSizes()

            Dim Tags() As clsTags.eTags = Object_Tags.GetTags

            For Each Tag As clsTags.eTags In Tags
                Dim item As ToolStripButton = Me.TSLabelsTag.Items.Add(Tag.tagname)
                Try
                    item.Image = Object_Icons.bmpImageFromFile(Application.StartupPath & "\resources\images\" & Tag.icon)
                Catch ex As Exception
                    item.Image = Object_Icons.bmpImage("icon_unknow")
                End Try
                item.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
                item.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
                item.Name = Tag.tagname
                item.Text = "&" & Tag.key
                item.ToolTipText = Tag.description
                item.Tag = Tag.command
                item.ImageScaling = ToolStripItemImageScaling.None
                item.TextAlign = System.Drawing.ContentAlignment.MiddleRight
            Next
        Catch ex As Exception

        End Try
    End Sub
    Public Sub UnactiveTagMenu()
        For Each item As Object In Me.TSLabelsTag.Items
            If Not item.Tag Is Nothing Then
                item.Checked = False
            End If
        Next
    End Sub

    Public Sub ActiveTagMenu(ByVal TagName As String)
        For Each item As Object In Me.TSLabelsTag.Items
            If Not item.Tag Is Nothing Then
                If TagName = item.Name Then
                    item.Checked = True
                End If
            End If
        Next
    End Sub

    Private Sub ListFonts()
        Dim FuentesInstaladas As New Drawing.Text.InstalledFontCollection
        Dim Fuentes() As FontFamily = FuentesInstaladas.Families()
        For Each Fuente As FontFamily In Fuentes
            Me.cboFonts.Items.Add(Fuente.Name)
        Next
        Me.cboFonts.Text = "(" & General.Object_Language.Lang_Text("Font") & ")"
    End Sub

    Private Sub ListSizes()
        For i = 1 To 16
            Me.cboSizes.Items.Add(i)
        Next
        Me.cboSizes.Text = "(" & General.Object_Language.Lang_Text("Font Size") & ")"
    End Sub

    Private Sub cboFonts_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboFonts.SelectedIndexChanged
        If Not General.Control_Main.ActualEditor Is Nothing Then
            Try
                General.Control_Main.ActualEditor.ExecuteTagCommand("FontName", Me.cboFonts.Text)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub cboSizes_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboSizes.KeyUp
        If e.KeyCode = Keys.Enter Then
            cboSizes_SelectedIndexChanged(sender, e)
        End If
    End Sub

    Private Sub cboSizes_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSizes.LostFocus

    End Sub


    Private Sub cboSizes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSizes.SelectedIndexChanged
        If Not General.Control_Main.ActualEditor Is Nothing Then
            Try
                General.Control_Main.ActualEditor.ExecuteTagCommand("FontSize", Me.cboSizes.Text)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub TSLabelsTag_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles TSLabelsTag.ItemClicked
        If Not General.Control_Main.ActualEditor Is Nothing Then
            Try
                If Not AddEspetialTag(e.ClickedItem.Tag.ToString.ToUpper) Then
                    General.Control_Main.ActualEditor.ExecuteTagCommand(e.ClickedItem.Tag, Nothing)
                End If

            Catch ex As Exception
                MsgBox(ex.Message & " - " & e.ClickedItem.Tag, MsgBoxStyle.Critical, "Error")
            End Try
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.btnSave.Enabled = False
        Me.btnSaveAll.Enabled = False
        Me.SaveFileToolStripMenuItem.Enabled = False

        If BeeWitFocused = True Then
            Dim Editor As ctlEditor
            For i = 0 To General.Control_Main.tabContainer.Items.Count - 1
                If General.Control_Main.tabContainer.Items(i).ToString = "BeeWit.ctlEditor" Then
                    Me.btnSave.Enabled = True
                    Me.btnSaveAll.Enabled = True
                    Me.SaveFileToolStripMenuItem.Enabled = True
                End If
            Next
            Editor = General.Control_Main.ActualEditor
            If swTimer = 0 Then
                swTimer = 1
                If Not Editor Is Nothing Then

                    'click on left button mouse
                    Dim PosX As Long = (Cursor.Position.X - frmBeeWit.Location.X) - General.Control_Main.LeftPanel.Dimension
                    Dim PosY As Long = (Cursor.Position.Y - frmBeeWit.Location.Y) - (Me.MenuStrip1.Height + Me.TSFiles.Height + 60)
                    Dim ExitFromClick As Boolean = False
                    Dim KeyCode As Integer = GetAsyncKeyState(1)
                    If PosX > 0 Then
                        If PosX < Control_Main.tabContainer.Width - 15 Then
                            If PosY > 0 Then
                                If PosY < Control_Main.tabContainer.Height - 80 Then
                                    If DragingControl Then
                                        If KeyCode = 32768 And DragingControl Then

                                        Else
                                            If KeyCode = 0 And DragingControl Then
                                                General.Control_Main.ActualEditor.HTML.HTML_GetElementOnClientMouse()
                                                Control_Main.AddControlToDesigner(General.Control_Controls.LastControlSelected)
                                                DragingControl = False
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                    If KeyCode = -32767 Then '

                        If PosX > 0 Then
                            If PosX < Control_Main.tabContainer.Width - 15 Then
                                If PosY > 0 Then
                                    If PosY < Control_Main.tabContainer.Height - 80 Then
                                        Editor.HTML.SelectOverElement()
                                        Editor.HTML.ValidateAttributes()
                                    End If
                                End If
                            End If
                        End If
                    End If

                    If ExitFromClick = True Then
                        Exit Sub
                    End If

                    'click on right button mouse
                    If GetAsyncKeyState(2) = -32767 Then
                        Editor.HTML.ValidateAttributes()
                    End If

                    If Editor.btnCode.Visible = True Then 'see if enable verify code
                        If Editor.btnCode.Enabled = False Then
                            Me.btnVerify.Enabled = True
                        Else
                            Me.btnVerify.Enabled = False
                        End If
                    Else
                        Me.btnVerify.Enabled = False
                    End If
                    If Editor.btnDesign.Visible = True Then 'see if enable dezign properties, event a controls
                        If Editor.btnDesign.Enabled = False Then 'design selected
                            Me.TSLabelsTag.Enabled = True
                            General.Control_Properties.Enabled = True
                            General.Control_Eventos.Enabled = True
                            General.Control_Controls.Enabled = True
                        Else
                            Me.TSLabelsTag.Enabled = False
                            General.Control_Properties.Enabled = False
                            General.Control_Eventos.Enabled = False
                            General.Control_Controls.Enabled = False
                        End If
                    End If
                Else
                    Me.TSLabelsTag.Enabled = False
                    General.Control_Properties.Enabled = False
                    General.Control_Eventos.Enabled = False
                    General.Control_Controls.Enabled = False
                    Me.btnVerify.Enabled = False
                End If
                swTimer = 0
            End If
        End If
    End Sub

    Private Sub btnVerify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerify.Click
        Control_Console.AddMessages("Saved! " & General.Control_Main.ActualEditor.Name & ". ", ctlConsole.enumTypeMessages.isINFORMATION)
        General.Control_Main.ActualEditor.Save()
        Control_Console.AddMessages("Verifing " & General.Control_Main.ActualEditor.Name & "... ", ctlConsole.enumTypeMessages.isINFORMATION)
        General.Control_Console.Compilar("-l")
    End Sub

    Private Sub btnCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        General.Control_Main.ActualEditor.ExecuteTagCommand("Copy", Nothing)
    End Sub

    Private Sub btnPaste_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPaste.Click
        General.Control_Main.ActualEditor.ExecuteTagCommand("Paste", Nothing)
    End Sub

    Private Function AddEspetialTag(ByVal TagName As String) As Boolean
        AddEspetialTag = False
        If TagName.ToUpper = "TABLE" Then
            AddEspetialTag = True
            Dim frmEspetialTag As New frmHTMLControls
            frmEspetialTag.LoadHTMLSetting(TagName.ToUpper)
        End If
    End Function

    Private Sub menu_faqs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menu_faqs.Click
        Dim OnLineSupport As New ctlOnLineSupport
        Control_Main.tabContainer.Items.Add(Object_Language.Lang_Text("FAQs"), OnLineSupport)
        OnLineSupport.LoadPage("faqs")
    End Sub

    Private Sub menu_technical_manual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menu_technical_manual.Click
        Dim OnLineSupport As New ctlOnLineSupport
        Control_Main.tabContainer.Items.Add(Object_Language.Lang_Text("Technical Manual"), OnLineSupport)
        OnLineSupport.LoadPage("technicalmanual")
    End Sub

    Private Sub menu_user_manual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menu_user_manual.Click
        Dim OnLineSupport As New ctlOnLineSupport
        Control_Main.tabContainer.Items.Add(Object_Language.Lang_Text("User Manual"), OnLineSupport)
        OnLineSupport.LoadPage("usermanual")
    End Sub

    Private Sub menu_samples_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menu_samples.Click

        Dim OnLineSupport As New ctlOnLineSupport
        Control_Main.tabContainer.Items.Add(Object_Language.Lang_Text("Samples"), OnLineSupport)
        Control_Main.tabContainer.Items(Control_Main.tabContainer.Items.Count - 1).Name = Object_Language.Lang_Text("Samples")
        OnLineSupport.LoadPage("samples")
    End Sub

    Private Sub menu_error_notifications_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menu_error_notifications.Click
        Dim OnLineSupport As New ctlOnLineSupport
        Control_Main.tabContainer.Items.Add(Object_Language.Lang_Text("Error Notifications"), OnLineSupport)
        OnLineSupport.LoadPage("errornotifications")
    End Sub

    Private Sub menu_about_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menu_about.Click
        Dim About As New frmAbout
        About.ShowDialog()
    End Sub

    Private Sub btnUndo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUndo.Click
        General.Control_Main.ActualEditor.ExecuteTagCommand("Undo", Nothing)
    End Sub

    Private Sub btnRedo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRedo.Click
        General.Control_Main.ActualEditor.ExecuteTagCommand("Redo", Nothing)
    End Sub

    Private Sub SaveFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFileToolStripMenuItem.Click
        Control_Main.Save()
    End Sub

    Private Sub StopDebuggingToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles StopDebuggingToolStripMenuItem.Click
        _objDebug.StopDebugging()
    End Sub

    Private Sub StepOutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles StepOutToolStripMenuItem.Click
        _objDebug.StepOut()
    End Sub

    Private Sub StepOverToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles StepOverToolStripMenuItem.Click
        _objDebug.StepOver()
    End Sub

    Private Sub StepIntoToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles StepIntoToolStripMenuItem.Click
        _objDebug.StepInto()
    End Sub

    Private Sub InspectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InspectToolStripMenuItem.Click
        _objDebug.InspectSelection()
    End Sub

    Private Sub ListenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListenToolStripMenuItem.Click
        Run()
    End Sub

    Private Sub btnDebug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDebug.Click
        Run()
    End Sub

    Private Sub Run()
        If _objDebug.IsRunning Then
            _objDebug.Run()
        Else
            _objDebug.listenForConnection()
        End If
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        _objDebug.StopDebugging()
    End Sub

    Private Sub btnInspect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInspect.Click
        _objDebug.InspectSelection()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        End
    End Sub

    Sub ProjectToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
        General.Control_Main.addpanel("explorer")
    End Sub

    Sub PropertiesToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
        General.Control_Main.addpanel("properties")
    End Sub

    Sub ControlsToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
        General.Control_Main.addpanel("controls")
    End Sub

    Sub EventsToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
        General.Control_Main.addpanel("events")
    End Sub

    Sub DebugToolStripMenuItem1Click(ByVal sender As Object, ByVal e As EventArgs)
        General.Control_Main.addpanel("debug")
    End Sub

    Sub ConsoleToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
        General.Control_Main.addpanel("console")
    End Sub

    Sub DebugToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Sub InspectToolStripMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
        _objDebug.InspectSelection()
    End Sub
End Class
